package daizuo;

import daizuo.bean.Cust;
import daizuo.utils.Readerinfo;
import daizuo.utils.WriteInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author cyt
 * @create 2020-07-21 12:25
 */
public class UserMenu {
    public static void addUser() {
        System.out.println("添加客户信息");
        System.out.println("id");
        String custNo = new Scanner(System.in).next();
        System.out.println("积分");
        String custScore = new Scanner(System.in).next();
        System.out.println("生日");
        String custBirth = new Scanner(System.in).next();
        Cust cust = new Cust(custNo, custBirth, custScore);

        WriteInfo.write("cust", cust.toString(), true);

        UserMenu();
    }

    public static void selectUser() {
        System.out.println("查找客户信息");
        System.out.println("输入 客户 id; ");
        String id = new Scanner(System.in).nextLine();
        ArrayList<String> cust = Readerinfo.read("cust");
        Cust cust1 = new Cust();
        String ans = null;
        cust1.setCustNo(id);
        for (int i = 0; i < cust.size(); i++) {
            String s = cust.get(i);
            String[] split = s.split(",");
            if (split[1].equals(id)) {
                cust1.setCustBirth(split[3]);
                cust1.setCustScore(split[5]);
                ans = cust1.toString();
            }
        }
        System.out.println("查询结果； ");
        System.out.println(ans);
        System.out.println("是否继续查询； （y / n ）");
        String isContinue = new Scanner(System.in).nextLine();
        if ("y".equals(isContinue)) {
            selectUser();
        } else {
        }
        UserMenu();
    }

    public static void updateUser() {
        System.out.println("修改客户信息, 输入 id ");
        String id = new Scanner(System.in).next();
        try {
            updateUserMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void selectUpdateUser(String id) throws IOException {
        int uA1 = 1;
        int uA2 = 2;
        int uA3 = 3;
        System.out.println("请选择，输入数字：  (非菜单数字则返回上一级)");
        int select = new Scanner(System.in).nextInt();
        if (select == uA1) {
            updataBirthday(id);
        } else if (select == uA2) {
            updataSource(id);
        } else if (select == uA3) {
            updateUser();
        } else {
            Menu.mainMenu();
        }
    }

    private static void updataSource(String id) {
        System.out.println("请输入积分； ");
        String source = new Scanner(System.in).nextLine();

        ArrayList<String> cust = Readerinfo.read("cust");

        Cust cust1 = new Cust();

        cust1.setCustNo(id);
        for (int i = 0; i < cust.size(); i++) {
            String s = cust.get(i);
            String[] split = s.split(",");
            if (split[1].equals(id)) {
                cust1.setCustBirth(split[3]);
                cust1.setCustScore(source);
                cust.set(i, cust1.toString());
            }
        }

        for (int i = 0; i < cust.size(); i++) {
            String s = cust.get(i);
            WriteInfo.write("cust", s, true);
        }
        UserMenu();
    }

    private static void updataBirthday(String id) {
        System.out.println("请输入生日； ");
        String birthday = new Scanner(System.in).nextLine();

        ArrayList<String> cust = Readerinfo.read("cust");

        Cust cust1 = new Cust();

        cust1.setCustNo(id);
        for (int i = 0; i < cust.size(); i++) {
            String s = cust.get(i);
            String[] split = s.split(",");
            if (split[1].equals(id)) {
                cust1.setCustBirth(birthday);
                cust1.setCustScore(split[5]);
                cust.set(i, cust1.toString());
            }
        }

        for (int i = 0; i < cust.size(); i++) {
            String s = cust.get(i);
            WriteInfo.write("cust", s, true);
        }
        UserMenu();
    }

    public static void updateUserMenu(String id) throws IOException {
        System.out.println("修改客户信息");
        System.out.println("------------------");
        System.out.println("1、修改生日");
        System.out.println("2、修改积分");
        System.out.println("------------------");
        selectUpdateUser(id);
    }

    public static void showAllUser() {
        System.out.println("所有客户信息");
        ArrayList<String> cust = Readerinfo.read("cust");
        for (int i = 0; i < cust.size(); i++) {
            System.out.println(cust.get(i));
        }
        UserMenu();
    }


    public static void UserMenu() {
        System.out.println("------------------");
        System.out.println("1、显示所有客户信息");
        System.out.println("2、添加");
        System.out.println("3、修改");
        System.out.println("4、查询");
        System.out.println("------------------");
        selectUserAdmin();
    }

    public static void selectUserAdmin() {
        int uA1 = 1;
        int uA2 = 2;
        int uA3 = 3;
        int uA4 = 4;
        System.out.println("请选择，输入数字：  (非菜单数字则返回上一级)");
        int select = new Scanner(System.in).nextInt();
        if (select == uA1) {
            showAllUser();
        } else if (select == uA2) {
            addUser();
        } else if (select == uA3) {
            updateUser();
        } else if (select == uA4) {
            selectUser();
        } else {
            try {
                Menu.mainMenu();
            } catch (Exception e) {

            }
        }
    }
}
